package com.shen.eduservice.controller;


import com.shen.commonutils.Result;
import com.shen.eduservice.entity.EduChapter;
import com.shen.eduservice.entity.chapter.ChapterVo;
import com.shen.eduservice.service.EduChapterService;
import com.shen.eduservice.service.EduSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author shenkaituo
 * @since 2020-06-21
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {
    @Autowired
    EduChapterService chapterService;
    @GetMapping("getChapterVideo/{courseId}")
    //查询某一课程的章节目录
    public Result getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> chapterList=chapterService.getChapterVideoByCourseId(courseId);
        return Result.ok().data("list",chapterList);
    }
   //添加章节
   @PostMapping("addChapter")
   public  Result addChapter(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);
        return Result.ok();

   }
   //根据id 得到章节信息
    @GetMapping("getChapterInfo/{chapterId}")
    public  Result getChapterInfo(@PathVariable String chapterId){
        EduChapter chapter = chapterService.getById(chapterId);
        return Result.ok().data("chapter",chapter);
    }
   //修改
   @PostMapping("updateChapter")
   public  Result updateChapterInfo(@RequestBody EduChapter eduChapter){
        System.out.println("updateChapter---------");
       boolean update = chapterService.updateById(eduChapter);
       System.out.println("update is"+update);
       if(update){
           return Result.ok();
       }
       else {
           return Result.error();
       }

   }
   //删除 章节下面有小节 1，级联删除，2，如果删除的下面有小节不让删除
    @DeleteMapping("{chapterId}")
    public  Result deleteChapter(@PathVariable String chapterId){
        //第二种
        System.out.println("delete --------------");
        System.out.println(chapterId);
        chapterService.deleteChapter(chapterId);
        return Result.ok();
    }


}

